package com.it.ems.mapper;

import com.it.ems.entity.Role;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface RoleMapper {
    @Select("SELECT * FROM roles WHERE id = #{id}")
    Role findById(Long id);
    
    @Select("SELECT * FROM roles WHERE name = #{name}")
    Role findByName(String name);
    
    @Select("SELECT * FROM roles")
    List<Role> findAll();
    
    @Insert("INSERT INTO roles (name, description, created_at, updated_at) " +
            "VALUES (#{name}, #{description}, NOW(), NOW())")
    int insert(Role role);
    
    @Update("UPDATE roles SET name = #{name}, description = #{description}, updated_at = NOW() " +
            "WHERE id = #{id}")
    int update(Role role);

    @Select("SELECT r.* FROM roles r JOIN user_roles ur ON r.id = ur.role_id WHERE ur.user_id = #{userId}")
    List<Role> findRolesByUserId(@Param("userId") Long userId);
} 